package artid.com.arttogo.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import artid.com.arttogo.R;
import artid.com.arttogo.service.AudioService;
import artid.com.arttogo.utils.MySharedPreferences;

public class BackGroundMusicActivity extends BaseActivity implements
        OnClickListener {
    MySharedPreferences sharedPreferences;
    View mViewOpen, mViewChocie, mViewMusic3, mViewYz, mViewMl, mViewCs, mViewQx;
    TextView textopen, textMusicSong;

    int intmusic = 0;
    String musicState = "0";
    // 使用ServiceConnection来监听Service状态的变化
    private ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub
            MainActivity.audioService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
            // 这里我们实例化audioService,通过binder来实现
            MainActivity.audioService = ((AudioService.AudioBinder) binder).getService(intmusic);

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_backmusic);
        mTittle = (TextView) findViewById(R.id.tittle_text);
        mTittle.setText("背景音乐");
        findViewById(R.id.back_btn).setOnClickListener(this);
        mViewMusic3 = (View) findViewById(R.id.view_music3);
        textopen = (TextView) findViewById(R.id.text_music_state);
        mViewOpen = (View) findViewById(R.id.view_backmusic_swith);
        mViewOpen.setOnClickListener(this);
        mViewChocie = (View) findViewById(R.id.view_music_choic);
        mViewChocie.setOnClickListener(this);
        mViewYz = (View) findViewById(R.id.musicyz);
        mViewYz.setOnClickListener(this);
        mViewCs = (View) findViewById(R.id.musiccs);
        mViewCs.setOnClickListener(this);
        mViewMl = (View) findViewById(R.id.musicml);
        mViewMl.setOnClickListener(this);
        mViewQx = (View) findViewById(R.id.musicqx);
        mViewQx.setOnClickListener(this);
        textMusicSong = (TextView) findViewById(R.id.music_songname);
//		audioService = MainActivity.audioService;

        sharedPreferences = MySharedPreferences.getMySharedPreferences(BackGroundMusicActivity.this);
        String state = sharedPreferences.getValue(sharedPreferences.TagisPlayMusic, "0");
        if (state != null) {
            musicState = state;
            if (musicState.equals("0")) {
                textopen.setText("关");
            } else {
                textopen.setText("开");
            }
        }
        int i = Integer.valueOf(sharedPreferences.getValue(sharedPreferences.TagMusicName, "0"));
        if (i == 2) {
            textMusicSong.setText("棹碧涛春水路");
        } else if (i == 1) {
            textMusicSong.setText("茉莉花");
        } else if (i == 0) {
            textMusicSong.setText("渔舟唱晚");
        }

    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Intent intent = new Intent();
        intent.setClass(this, AudioService.class);
        switch (arg0.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.view_backmusic_swith:
                if (textopen.getText().toString().equals("开")) {
                    textopen.setText("关");
                    if (MainActivity.audioService != null) {
                        MainActivity.audioService.pauseMusic();
                        MainActivity.setMusicStop = true;
                    }
//				unbindService(conn);
                    sharedPreferences.setValue(sharedPreferences.TagisPlayMusic, "0");
                } else {
                    textopen.setText("开");
                    MainActivity.setMusicStop = false;
                    startService(intent);
                    bindService(intent, conn, Context.BIND_AUTO_CREATE);
                    sharedPreferences.setValue(sharedPreferences.TagisPlayMusic, "1");
                }
                break;

            case R.id.view_music_choic:
                if (mViewMusic3.getVisibility() == View.VISIBLE) {
                    mViewMusic3.setVisibility(View.GONE);
                } else {
                    mViewMusic3.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.musiccs:
                setmusic(2);
                mViewMusic3.setVisibility(View.GONE);
                break;
            case R.id.musicml:
                setmusic(1);
                mViewMusic3.setVisibility(View.GONE);
                break;
            case R.id.musicyz:
                setmusic(0);
                mViewMusic3.setVisibility(View.GONE);
                break;
            case R.id.musicqx:
                mViewMusic3.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }

    void setmusic(int i) {
        textopen.setText("开");
        sharedPreferences.setValue(sharedPreferences.TagisPlayMusic, "1");
        Intent intent = new Intent();
        intent.setClass(this, AudioService.class);
        if (MainActivity.audioService != null) {
            MainActivity.audioService.pauseMusic();
            MainActivity.setMusicStop = true;
        }else{
            textopen.setText("开");
            MainActivity.setMusicStop = false;
            startService(intent);
            bindService(intent, conn, Context.BIND_AUTO_CREATE);
        }
        onClick(textopen);
        if (i == 2) {
            textMusicSong.setText("棹碧涛春水路");
        } else if (i == 1) {
            textMusicSong.setText("茉莉花");
        } else if (i == 0) {
            textMusicSong.setText("渔舟唱晚");
        }
        if (MainActivity.audioService != null) {
            MainActivity.audioService.setMusic(i);
            sharedPreferences.setValue(sharedPreferences.TagMusicName, String.valueOf(i));
        }

    }

}
